/**
 * 
 * @authors Riche
 * @date    2016-07-06 14:39:22
 * @version 1.1.0
 */
var backUrl = $.getUrlVar("back"),
    id = $.getUrlVar("id");

var vm = new Vue({
    el: "#app",
    data: {
        title: '',
        addShow: false,
        addphotoshow: false,
        contentPicture: [],
        classify: [],
        circleID: 0,
        typeID: 0,
        askContent: '',
    },
    compiled: function() {
        var _self = this;
        _self.loadPhoto(); //调用图片预加载

        //圈子列表
        var url = 'circle_getCircleList';
        Common.ajaxCall({
            url: url,
            datatype: 'json',
            type: 'post',
            data: {}
        }, function(data) {
            if (data.error_code == 0) {
                _self.classify = data.data;
            } else {
                alert(data.msg);
            }
        });



        // 选择图片
        document.getElementById('pic').onchange = function (e) {
            event = event || e;
            var img = event.target.files[0];
            // 判断是否图片  
            if (!img) {
                return;
            }
            // 判断图片格式  
            if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {
                alert('图片只能是jpg,gif,png');
                return;
            }

            //定义图片参数
            var uploadFile = new FormData();
            uploadFile.append("uploadFile", img);
            //var uploadFile = new FormData(img);

            //接口方面的更新，现在上传图片必须得有accessToken等
            getaccessToken(function(token) {

                var url = 'circle_getFolderId';
                Common.ajaxCall({
                    url: url,
                    datatype: 'json',
                    type: 'post'
                }, function(data) {
                    if (data.error_code == 0) {
                        uploadFile.append("folderId", data.data[0].folderId);
                        uploadFile.append("accessToken", token);
                        uploadFile.append("tags", _self.title);

                        //data = Common.jsonEncode(data);
                        Common.ajaxSubmitPic({
                            url: 'upload_file',
                            data: uploadFile,
                        }, function(data) {
                            if (data.error_code == 0) {
                                /*
                                var dom = '<li class="imgBox j_1" ><i class="closeImg" onclick="delImg(this);"></i><img src="' + data.data[0].content + '"  class="loadImg"></li>';
                                $('#dom').append(dom);
                                //$('#pic1').attr('src',image);
                                _self.contentPicture.push(data.msg);
                                myJS.imgScale('.j_1', '1', '1');
                                myJS.ImgFull('.loadImg','images/liby.png');
                                //vm.addReplyPic(image);
                                */
                                //添加图片到内容中去
                                //$('#content').htmlarea('tofocus');
                                $('#content').htmlarea('image', data.data[0].content);
                            } else {
                                alert(data.msg);
                            }
                        });
                    } else {
                        alert(data.msg);
                    }
                });
            });

            /*
            var data = {};
            data = Common.jsonEncode(data);
            Common.ajaxSubmit({
                target: "#am_form",
                url: 'common_uploadPic',
                datatype: 'json',
                type: 'get',
                data: data
            }, function(data) {
                if (data.error_code == 0) {
                    var dom = '<li class="imgBox j_1" ><i class="closeImg" onclick="delImg(this);"></i><img src="' + data.msg + '"  class="loadImg"></li>';
                    $('#dom').append(dom);
                    //$('#pic1').attr('src',image);
                    _self.contentPicture.push(data.msg);
                    myJS.imgScale('.j_1', '1', '1');
                    myJS.ImgFull('.loadImg','images/liby.png');
                    //vm.addReplyPic(image);
                } else {
                    alert(data.msg);
                }
            });
            */
        }

    },
    methods: {
        publish: function() {
            var _self = this;
            var title = $("#title").val();
            var content = $("#content").val();
            content = $('#content').htmlarea('html'); //取内容
            if (title == "") { //如果没填写内容
                alert('请填写内容');
                $('#title').focus();
                return false;
            }

            var data = {
                    circleID: _self.circleID,
                    typeID: _self.typeID,
                    contentPicture: _self.contentPicture.join(','),
                    title: title,
                    content: content
                }
                //data = Common.jsonEncode(data);
            Common.ajaxCall({
                url: 'circle_addTopic',
                datatype: 'json',
                type: 'post',
                data: data
            }, function(data) {
                if (data.error_code == 0) {
                    alert(data.msg);
                    //延迟跳转
                    setTimeout(function() {
                        window.location.href = decodeURIComponent(backUrl);
                    }, 500);
                } else {
                    alert(data.msg);
                }
            });
        },
        addphoto: function() {
            if (this.addphotoshow) {
                this.addphotoshow = false;
            } else {
                this.addphotoshow = true;
            }
        },
        loadPhoto: function() {
            myJS.imgScale('.j_1', '1', '1');
            myJS.imgScale('.j_1511', '15', '11');
            myJS.ImgFull('.loadImg', 'images/liby.png');
        },
        selectPic: function() {
            //选择图片
            $("#pic").click();
        },
        back: function() {
            window.location.href = decodeURIComponent(backUrl);
        }
    },
    watch: {
        "willShow": function() {
            this.loadPhoto();
        },
        "classify": function() {
            var _self = this;
            //初始化mobilescroll组件
            var opt = {
                'default': {
                    theme: 'default',
                    mode: 'scroller',
                    display: 'bottom',
                    animate: 'fade',
                    placeholder: '选择板块',
                    //交换按钮位置
                    buttons: [
                        'set'
                    ],

                    headerText: function(valueText) {
                        return "选择板块";
                    },
                    // 选择之后赋值
                    onSelect: function(e) {

                        _self.title = $('#names_dummy').val();
                    },
                    onBeforeShow: function(inst) {
                        if (id && id.length > 0) {
                            inst.setValue(_self.circleID, true);
                        }
                    }
                },
                'select-opt': {
                    preset: 'select',
                    group: true
                }
            }
            $('.block-select-opt').scroller($.extend(opt['select-opt'], opt['default']));
            var child, $option;
            $option = $('#names').children('optgroup').children('option');
            if (id && id.length > 0) {
                $.each($option, function(k, v) {
                    if (v.value == id) {
                        _self.circleID = id;
                        _self.typeID = $(v).parent().attr("value");
                        $('#names_dummy').val(v.text);
                        return false;
                    }
                });
            } else {
                child = $option.eq(0);
                _self.circleID = child.val();
                _self.typeID = child.parent().attr("value");
            }

            $('#names').change(function() {
                _self.circleID = $(this).children('optgroup').children('option:selected').val();
                _self.typeID = $(this).children('optgroup').children('option:selected').parent().attr("value");

            });

            _self.title = $('#names_dummy').val(); //初始化赋值

        }
    }
});